
package com.example.demo.modules.sys.controller;

import com.example.demo.core.entity.ProcessResult;
import com.example.demo.core.property.PropertiesValue;
import com.example.demo.modules.sys.model.SysRole;
import com.example.demo.modules.sys.model.SysUser;
import com.example.demo.modules.sys.service.RoleService;
import com.example.demo.modules.sys.service.UserService;
import com.example.demo.modules.sys.vo.SysUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * @author qjp
 * @since 2015-12-19 11:10
 */
@RestController
@RequestMapping("/sys/login")
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;

    @Autowired
    private PropertiesValue propertiesValue;

    @RequestMapping(value = "/toLogin")
    public ProcessResult toLogin(SysUser sysUser, HttpServletRequest request) {
        ProcessResult result=new ProcessResult();
        /*if(!"wwwc".equalsIgnoreCase(verify)){
            result.setMess("验证码输入错误！");
            result.setResultStat(ProcessResult.ERROR);
            return result;
        }*/
        sysUser.setStatus(0);
        SysUser user=userService.getUser(sysUser);
        if(user==null){
            result.setMess("用户名或密码错误！");
            result.setResultStat(ProcessResult.ERROR);
            return result;
        }
        SysUserVo sysUserVo=new SysUserVo();
        BeanUtils.copyProperties(user,sysUserVo);
        List<SysRole> roles=roleService.findByUserId(sysUserVo.getId());
        if(roles!=null && roles.size()>0){
            sysUserVo.setSysRole(roles.get(0));
        }else{
            sysUserVo.setSysRole(new SysRole().setDefaultRole());
        }
        request.getSession().setAttribute("user",sysUserVo);
        result.setResultStat(ProcessResult.SUCCESS);
        result.setData(user);
        return result;
    }

    @RequestMapping(value = "/modifyPWD")
    public ProcessResult modifyPWD(String oldpwd, String password,HttpSession session) {
        SysUser user=(SysUser) session.getAttribute("user");
        ProcessResult result=new ProcessResult();
        if(!oldpwd.equals(user.getPassword())){
            result.setMess("旧密码有误！");
            result.setResultStat(ProcessResult.ERROR);
            return result;
        }
        user.setPassword(password);
        int i = userService.save(user);
        SysUserVo sysUserVo=new SysUserVo();
        BeanUtils.copyProperties(user,sysUserVo);
        List<SysRole> roles=roleService.findByUserId(sysUserVo.getId());
        if(roles!=null && roles.size()>0){
            sysUserVo.setSysRole(roles.get(0));
        }else{
            sysUserVo.setSysRole(new SysRole().setDefaultRole());
        }
        session.setAttribute("user",sysUserVo);
        result.setData(i);
        result.setResultStat(ProcessResult.SUCCESS);
        return result;
    }

    @RequestMapping(value = "/showVerify",method = RequestMethod.POST)
    public  ProcessResult showVerify(String email){
        return  userService.showVerify(email);
    }

    @RequestMapping(value = "/register",method = RequestMethod.POST)
    public  ProcessResult register(SysUserVo vo){
        return  userService.saveUser(vo);
    }

    @PostMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败，请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = propertiesValue.avatarPath;
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            //LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            //LOGGER.error(e.toString(), e);
            e.getStackTrace();
        }
        return "上传失败！";
    }
}
